//PSRM - Kalmoe Figure 1, Study 1 & Study 2

***Remember the order of variables is X, Z, W. ***
***On the graph, W represents the different lines.  ***

***Nathan Note: This file attempts to combine parts of the 2- and 3-way ME plots from Golder. 
***The goal is to show the effect of violent metaphors on the change in partisan polarization on issues.
***Estimates 3-way model, subtracts difference between 2 lines, calculates SEs similar to 2-way model

* y = x z w xz xw zw xzw*
* x(Treat), z(aggress), w(PID), y(dependent variable)

reg vote viol aggress commitment violxaggress violxcommitment aggressxcommitment violxaggxcommit

#delimit ;
*Estimating marginal effect of treatment, conditional on TA and PID*;

egen zmin = min(aggress);
egen zmax = max(aggress);

gen z0 = (((_n-1)/(20-1))*(zmax-zmin))+zmin in 1/20;
generate Z=z0;

*     ****************************************************************  *;
*       Generate the values of W for which you want to calculate the    *;
*       marginal effect (and standard errors) of X on Y.                *;
*     ****************************************************************  *;

scalar W0=0;
scalar W1=1;

*     ****************************************************************  *;
*       Grab elements of the coefficient and variance-covariance matrix *;
*       that are required to calculate the marginal effect and standard *;
*       errors.                                                         *;
*     ****************************************************************  *;

matrix b=e(b);
matrix V=e(V);

scalar b1=b[1,1];
scalar b2=b[1,2];
scalar b3=b[1,3];
scalar b4=b[1,4];
scalar b5=b[1,5];
scalar b6=b[1,6];
scalar b7=b[1,7];

scalar varb1=V[1,1];
scalar varb2=V[2,2];
scalar varb3=V[3,3];
scalar varb4=V[4,4];
scalar varb5=V[5,5];
scalar varb6=V[6,6];
scalar varb7=V[7,7];

scalar covb1b4=V[1,4];
scalar covb1b5=V[1,5];
scalar covb1b7=V[1,7];
scalar covb4b5=V[4,5];
scalar covb4b7=V[4,7];
scalar covb5b7=V[5,7];

scalar list b1 b2 b3 b4 b5 b6 b7 varb1 varb2 varb3 varb4 varb5 varb6 varb7 
           covb1b4 covb1b5 covb1b7 covb4b5 covb4b7 covb5b7;
           
*     ****************************************************************  *;
*       We want to calculate the marginal effect of X on Y for all      *;
*       Z values of the modifying variable Z. We also want to          *;
*       calculate this marginal effect as Z changes for specific values *;
*       of the second modifying variable W.  In the code below, we      *;
*       calculate the marginal effect of X on Y for all values of Z     *;
*       when W=0, when W=1, when W=2, and when W=3.                     *;
*     ****************************************************************  *;

gen conb0=b1+b4*Z+b5*W0+b7*(Z*W0);
gen conb1=b1+b4*Z+b5*W1+b7*(Z*W1);

gen conbdif=conb1-conb0;

*     ****************************************************************  *;
*       Calculate the standard errors for the marginal effect of X on Y *;
*       for all Z values of the modifying variable Z. Do this for the  *;
*       case when W=0, when W=1, when W=2, and when W=3.                *;
*     ****************************************************************  *;

gen conse0=sqrt(varb1
               + varb4*(Z^2) + varb5*(W0^2) + varb7*(Z^2)*(W0^2)
               + 2*Z*covb1b4 + 2*W0*covb1b5 + 2*Z*W0*covb1b7 + 2*Z*W0*covb4b5
               + 2*W0*(Z^2)*covb4b7 + 2*(W0^2)*Z*covb5b7);

gen conse1=sqrt(varb1
               + varb4*(Z^2) + varb5*(W1^2) + varb7*(Z^2)*(W1^2)
               + 2*Z*covb1b4 + 2*W1*covb1b5 + 2*Z*W1*covb1b7 + 2*Z*W1*covb4b5
               + 2*W1*(Z^2)*covb4b7 + 2*(W1^2)*Z*covb5b7);


			   			   
gen consedif=sqrt(conse0^2 + conse1^2);
			   
		
			
gen a=1.645*consedif;
 
gen upper=conbdif+a;
 
gen lower=conbdif-a;
			

graph twoway line conbdif   Z, clwidth(medium) clcolor(blue) clcolor(black)
        ||   line upper  Z, clpattern(dash) clwidth(thin) clcolor(black)
        ||   line lower  Z, clpattern(dash) clwidth(thin) clcolor(black)
        ||   ,   
		 xlabel(0(.2)1, labsize(5)) 
             ylabel(-1(.5)2, labsize(5))
		 legend(off)
             yline(0, lcolor(black))   
             xtitle(Trait Aggression, size(5)  )
             xsca(titlegap(2))
             ysca(titlegap(2))
             ytitle(Marginal Treatment FX on Impact of Motives, size(4))
             scheme(s2mono) graphregion(fcolor(white));
             
drop zmax zmin z0 Z conb0 conb1 conbdif conse0 conse1 consedif a upper lower;
